﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using TFXK.Model;
using TFXK.BLL;
using TFXK.Common;
using log4net;

public partial class admin_LinesManage_Action : BasePage
{
    private static readonly ILog log = LogManager.GetLogger(typeof(admin_LinesManage_Action));
    private static readonly TravelLinesBLL bll = new TravelLinesBLL();
    private static readonly CategoryBLL bllCategory = new CategoryBLL();
    private static readonly PicturesBLL bllPictures = new PicturesBLL();
    public static string codeNo = "lyxl";
    public int i = 0;

    #region 加载
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                InitAction();
            }
        }
        catch (Exception ex)
        {
            log.Error(ex.Message);
        }

    }
    #endregion

    #region 绑定动作
    private void InitAction()
    {
        if (Request.QueryString["codeNo"] != null)
        {
            codeNo = Request.QueryString["codeNo"] + "";
        }
        if (Request.QueryString["type"] != null)
        {
            BindSelType();
            string type = Request.QueryString["type"].ToString();
            string title = string.Empty;// 设置标题
            // 判断动作
            this.Title = title;
            this.hdfAction.Value = type;
            switch (type)
            {
                case "add":
                    imgPic.ImageUrl = "../../images/nopic.jpg";
                    title = "旅游线路添加";
                    break;
                case "modify":
                    if (Request.QueryString["id"] != null)
                    {
                        title = "旅游线路修改";
                        int id = int.Parse(Request.QueryString["id"].ToString());
                        InitData(id);
                    }
                    else
                    {
                        Response.Redirect("Default.aspx", false);
                    }
                    break;

            }

        }
        else
        {
            Response.Redirect("Default.aspx", false);
        }
    }
    #endregion

    #region 绑定
    private void InitData(int id)
    {
        TravelLines model = bll.GetModel(id);
        if (model != null)
        {
            txt_tourName.Text = model.tourName;
            txtContent.Text = model.travelDes;
            txtCycleDays.Text = model.cycleDays + "";
            txtFeatureDes.Value = model.featureDes;
            txtOrderby.Text = model.orderID+"";
            txtPrice.Text = model.price + "";
            txtPubDate.Text = model.pubDate.Value.ToString("yyyy-MM-dd");
            txtStarCity.Text = model.starCity;
            txtStartDate.Text = model.startDate;
            txtTitle.Text = model.title;

            chkCJTuiJian.Checked = model.isCJTTuiJian == 1 ? true : false;
            chkIsGNHot.Checked = model.isGNHot == 1 ? true : false;
            chkIsShowHH.Checked = model.isShowHH == 1 ? true : false;
            chkIsSHowJD.Checked = model.isShowJD == 1 ? true : false;
            chkIsSNHot.Checked = model.isSNHot == 1 ? true : false;
            chkIsZBTravel.Checked = model.isZBTravel == 1 ? true : false;
            chkTeJia.Checked = model.isTejia == 1 ? true : false;
            chkTuiJian.Checked = model.isTuijian == 1 ? true : false;

            if (model.subjectID == 0)
            {
                ddlSubject.SelectedIndex = 0;
            }
            else {
                ddlSubject.SelectedValue = model.subjectID + "";
            }

            if (model.travelShowType == 0)
            {
                this.ddlTravelShowType.SelectedIndex = 0;
            }
            else
            {
                ddlTravelShowType.SelectedValue = model.travelShowType + "";
            }
            ddlLineType.SelectedValue = model.travelType + "";

            string code = "";
            if (ddlLineType.SelectedItem.Text.Equals("国内旅游"))
            {
                code = "gnly_city";
            }
            else if (ddlLineType.SelectedItem.Text.Equals("出境旅游"))
            {
                code = "cjly_city";
            }
            else if (ddlLineType.SelectedItem.Text.Equals("自由行"))
            {
                code = "zyx_city";
            }
            else if (ddlLineType.SelectedItem.Text.Equals("豪华油轮"))
            {
                code = "hhyl_city";
            }

            DataSet ds = bllCategory.GetNextNodeByCode(code);
            this.ddlPro.DataSource = ds;
            ddlPro.DataTextField = "title";
            ddlPro.DataValueField = "codeNo";
            ddlPro.DataBind();

            ddlPro.SelectedValue = bllCategory.GetModel(bllCategory.GetModel(model.endCity.Value).parentID).codeNo;

            ds = bllCategory.GetNextNodeByCode(ddlPro.SelectedValue);
            this.ddlCity.DataSource = ds;
            this.ddlCity.DataTextField = "title";
            ddlCity.DataValueField = "id";
            ddlCity.DataBind();
            ddlCity.SelectedValue = model.endCity + "";
            

            imgPic.ImageUrl = string.IsNullOrEmpty(model.imagePath) ? "../../images/nopic.jpg" : "../../uploads/" + model.imagePath;
            hdfPic.Value = model.imagePath;
            
           

        }
    }
    #endregion

    #region 添加/修改
    protected void ibtnAdd_Click(object sender, EventArgs e)
    {
        //验证代码
        try
        {
            TFXK.Model.TravelLines model = new TFXK.Model.TravelLines();
            try
            {
                model.id = int.Parse(Request.QueryString["id"].ToString());
            }
            catch { model.id = 0; }
            model.title = txtTitle.Text.Trim();
            model.tourName = txt_tourName.Text.Trim();
            model.cycleDays = int.Parse(txtCycleDays.Text);
            model.endCity = int.Parse(ddlCity.SelectedValue);
            model.featureDes = txtFeatureDes.Value;
            model.imagePath = UpLoadImages();
            model.isCJTTuiJian = chkCJTuiJian.Checked ? 1 : 0;
            model.isGNHot = chkIsGNHot.Checked ? 1 : 0;
            model.isShowHH = chkIsShowHH.Checked ? 1 : 0;
            model.isShowJD = chkIsSHowJD.Checked ? 1 : 0;
            model.isSNHot = chkIsSNHot.Checked ? 1 : 0;
            model.isTejia = chkTeJia.Checked ? 1 : 0;
            model.isTuijian = chkTuiJian.Checked ? 1 : 0;
            model.isZBTravel = chkIsZBTravel.Checked ? 1 : 0;
            model.orderID = int.Parse(txtOrderby.Text);
            model.price = txtPrice.Text;
            model.pubDate = DateTime.Now;
            model.starCity = txtStarCity.Text;
            model.startDate = txtStartDate.Text;
            model.travelDes = txtContent.Text;
            if (ddlSubject.SelectedIndex == 0)
            {
                model.subjectID = 0;
            }
            else
            {
                model.subjectID = int.Parse(ddlSubject.SelectedValue);
            }

            if (this.ddlTravelShowType.SelectedIndex == 0)
            {
                model.travelShowType = 0;
            }
            else
            {
                model.travelShowType = int.Parse(ddlTravelShowType.SelectedValue);
            }
            model.travelType = int.Parse(ddlLineType.SelectedValue);

            model.theState = int.Parse(this.ddlState.SelectedValue);

            // 防止网页重复提交
            if (this.CheckRefreshed1.isRefreshed == false)
            {
                string type = this.hdfAction.Value.Trim();
                // 判断动作
                switch (type)
                {
                    case "add":
                        Add(model);
                        break;
                    case "modify":
                        Modify(model);
                        break;
                }
            }
        }
        catch (Exception ex)
        {
            log.Error(ex.Message);
            Msg.Show("网络错误!原因:" + ex.Message);
        }
    }
    #endregion

    #region 添加
    private void Add(TravelLines model)
    {
        int rows = bll.Add(model);
        if (rows > 0)
        {
            Msg.ShowAndRedirect("添加信息成功!", "Default.aspx?codeNo=" + codeNo);
        }
        else
        {
            Msg.Show("添加信息失败!");
        }
    }
    #endregion

    #region 修改
    private void Modify(TravelLines model)
    {
        bll.Update(model);
        Msg.ShowAndRedirect("修改信息成功!", "Default.aspx?codeNo=" + codeNo);
    }
    #endregion

    #region 返回
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx?codeNo=" + codeNo, false);
    }
    #endregion

    #region 绑定下拉列表
    private void BindSelType()
    {
        DataSet dss = bllCategory.GetAllChildByCodeNoPar("lyxl");
        ddlLineType.DataSource = dss.Tables[0];
        ddlLineType.DataValueField = "id";
        ddlLineType.DataTextField = "title";
        ddlLineType.DataBind();
        string code ="gnly_city";
        if (ddlLineType.SelectedItem.Text.Equals("国内旅游"))
        {
            code = "gnly_city";
        }
        else if (ddlLineType.SelectedItem.Text.Equals("出境旅游"))
        {
            code = "cjly_city";
        }
        else if (ddlLineType.SelectedItem.Text.Equals("自由行"))
        {
            code = "zyx_city";
        }
        else if (ddlLineType.SelectedItem.Text.Equals("豪华油轮"))
        {
            code = "hhyl_city";
        }

        DataSet ds = bllCategory.GetNextNodeByCode(code);
        this.ddlPro.DataSource = ds;
        ddlPro.DataTextField = "title";
        ddlPro.DataValueField = "codeNo";
        ddlPro.DataBind();


        ds = bllCategory.GetNextNodeByCode(ddlPro.SelectedValue);
        this.ddlCity.DataSource = ds;
        this.ddlCity.DataTextField = "title";
        ddlCity.DataValueField = "id";
        ddlCity.DataBind();


        ds = bllCategory.GetNextNodeByCode("ztly");
        this.ddlSubject.DataSource = ds;
        this.ddlSubject.DataTextField = "title";
        ddlSubject.DataValueField = "id";
        ddlSubject.DataBind();
        ddlSubject.Items.Insert(0, "---未分类---");

        ds = bllCategory.GetNextNodeByCode("lytjlb");
        this.ddlTravelShowType.DataSource = ds;
        this.ddlTravelShowType.DataTextField = "title";
        ddlTravelShowType.DataValueField = "id";
        ddlTravelShowType.DataBind();
        ddlTravelShowType.Items.Insert(0, "---未分类---");

    }
    #endregion



    #region 图片上传
    private string UpLoadImages()
    {
        string imgOldPath = hdfPic.Value;
        try
        {
            string saveDir = "../../uploads/"; //文件保存文件夹
            string serverFile = Server.MapPath(saveDir);
            int Nid = 0;
            foreach (DevExpress.Web.ASPxUploadControl.UploadedFile file in uploadControl.UploadedFiles)
            {
                if (!string.IsNullOrEmpty(file.PostedFile.FileName))
                {
                    if (!string.IsNullOrEmpty(imgOldPath))
                    {
                        try
                        {
                            string fullpath = Server.MapPath("../../uploads/" + imgOldPath);
                            System.IO.File.Delete(fullpath);
                        }
                        catch { }
                    }

                    string imgExt = file.FileName.Substring(file.FileName.IndexOf('.'));
                    string saveName = DateTime.Now.ToString("yyyyMMddhhmmss") + imgExt;

                    file.SaveAs(serverFile + saveName);
                    imgOldPath = saveName;
                }
            }
        }
        catch (Exception ex)
        {
            Msg.Show("网络错误:" + ex.Message);
        }
        return imgOldPath;
    }
    #endregion



    #region 联动 绑定城市下拉列表
    protected void ddlLineType_SelectedIndexChanged(object sender, EventArgs e)
    {
        string code = "gnly_city";
        try
        {
            if (ddlLineType.SelectedItem.Text.Equals("国内旅游"))
            {
                code = "gnly_city";
            }
            else if (ddlLineType.SelectedItem.Text.Equals("出境旅游"))
            {
                code = "cjly_city";
            }
            else if (ddlLineType.SelectedItem.Text.Equals("自由行"))
            {
                code = "zyx_city";
            }
            else if (ddlLineType.SelectedItem.Text.Equals("豪华油轮"))
            {
                code = "hhyl_city";
            }
            DataSet ds = bllCategory.GetNextNodeByCode(code);
            this.ddlPro.DataSource = ds;
            ddlPro.DataTextField = "title";
            ddlPro.DataValueField = "codeNo";
            ddlPro.DataBind();

            ddlPro_SelectedIndexChanged(sender, e);
        }
        catch
        {
        }
    }

    protected void ddlPro_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {

            if (!string.IsNullOrEmpty(ddlPro.SelectedValue + ""))
            {
                DataSet ds = bllCategory.GetNextNodeByCode(ddlPro.SelectedValue);
                this.ddlCity.DataSource = ds;
                this.ddlCity.DataTextField = "title";
                ddlCity.DataValueField = "id";
                ddlCity.DataBind();
            }
        }
        catch
        {

        }
    }
    #endregion
}
